package com.dianping.cat.home.service.client.transform;
import com.dianping.cat.home.service.client.entity.ClientReport;
import com.dianping.cat.home.service.client.entity.Domain;
import com.dianping.cat.home.service.client.entity.Method;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import com.dianping.cat.home.service.client.IVisitor;
public class DefaultNativeParser implements IVisitor {
  private DefaultLinker m_linker;

  public DefaultNativeParser(InputStream in) {
/*  15 */     this.m_linker = new DefaultLinker(true);




    this.m_in = new DataInputStream(in);
  }
  private DataInputStream m_in;

  public static ClientReport parse(byte[] data) { return parse(new ByteArrayInputStream(data)); }


  public static ClientReport parse(InputStream in) {
/*  28 */     DefaultNativeParser parser = new DefaultNativeParser(in);
    ClientReport clientReport = new ClientReport();

    try {
      clientReport.accept(parser);
/*  33 */     } catch (RuntimeException e) {
/*  34 */       if (e.getCause() == null || !(e.getCause() instanceof java.io.EOFException))
      {

/*  37 */         throw e;
      }
    }

/*  41 */     parser.m_linker.finish();
/*  42 */     return clientReport;
  }



  public void visitClientReport(ClientReport clientReport) {
    byte tag;
/*  49 */     if ((tag = readTag()) != -4) {
/*  50 */       throw new RuntimeException(String.format("Malformed payload, expected: %s, but was: %s!", new Object[] { Integer.valueOf(-4), Byte.valueOf(tag) }));
    }

/*  53 */     while ((tag = readTag()) != -1) {
/*  54 */       visitClientReportChildren(clientReport, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitClientReportChildren(ClientReport clientReport, int _field, int _type) {
/*  59 */     switch (_field) {
      case 1:
/*  61 */         clientReport.setDomain(readString());
        break;
      case 2:
/*  64 */         clientReport.setStartTime(readDate());
        break;
      case 3:
/*  67 */         clientReport.setEndTime(readDate());
        break;
      case 33:
/*  70 */         if (_type == 1) {
/*  71 */           Domain domain = new Domain();

/*  73 */           visitDomain(domain);
/*  74 */           this.m_linker.onDomain(clientReport, domain); break;
/*  75 */         }  if (_type == 2) {
/*  76 */           for (int i = readInt(); i > 0; i--) {
/*  77 */             Domain domain = new Domain();

/*  79 */             visitDomain(domain);
/*  80 */             this.m_linker.onDomain(clientReport, domain);
          }
        }
        break;
    }
  }



  public void visitDomain(Domain domain) {
    byte tag;
/*  91 */     while ((tag = readTag()) != -1) {
/*  92 */       visitDomainChildren(domain, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitDomainChildren(Domain domain, int _field, int _type) {
/*  97 */     switch (_field) {
      case 1:
/*  99 */         domain.setId(readString());
        break;
      case 2:
/* 102 */         domain.setTotalCount(readLong());
        break;
      case 3:
/* 105 */         domain.setFailureCount(readLong());
        break;
      case 4:
/* 108 */         domain.setFailurePercent(readDouble());
        break;
      case 5:
/* 111 */         domain.setSum(readDouble());
        break;
      case 6:
/* 114 */         domain.setAvg(readDouble());
        break;
      case 33:
/* 117 */         if (_type == 1) {
/* 118 */           Method method = new Method();

/* 120 */           visitMethod(method);
/* 121 */           this.m_linker.onMethod(domain, method); break;
/* 122 */         }  if (_type == 2) {
/* 123 */           for (int i = readInt(); i > 0; i--) {
/* 124 */             Method method = new Method();

/* 126 */             visitMethod(method);
/* 127 */             this.m_linker.onMethod(domain, method);
          }
        }
        break;
    }
  }



  public void visitMethod(Method method) {
    byte tag;
/* 138 */     while ((tag = readTag()) != -1) {
/* 139 */       visitMethodChildren(method, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitMethodChildren(Method method, int _field, int _type) {
/* 144 */     switch (_field) {
      case 1:
/* 146 */         method.setId(readString());
        break;
      case 2:
/* 149 */         method.setService(readString());
        break;
      case 3:
/* 152 */         method.setTotalCount(readLong());
        break;
      case 4:
/* 155 */         method.setFailureCount(readLong());
        break;
      case 5:
/* 158 */         method.setFailurePercent(readDouble());
        break;
      case 6:
/* 161 */         method.setSum(readDouble());
        break;
      case 7:
/* 164 */         method.setAvg(readDouble());
        break;
      case 8:
/* 167 */         method.setQps(readDouble());
        break;
      case 9:
/* 170 */         method.setTimeout(readDouble());
        break;
    }
  }

  private Date readDate() {
    try {
/* 177 */       return new Date(readVarint(64));
/* 178 */     } catch (IOException e) {
/* 179 */       throw new RuntimeException(e);
    }
  }

  private double readDouble() {
    try {
/* 185 */       return Double.longBitsToDouble(readVarint(64));
/* 186 */     } catch (IOException e) {
/* 187 */       throw new RuntimeException(e);
    }
  }

  private int readInt() {
    try {
/* 193 */       return (int)readVarint(32);
/* 194 */     } catch (IOException e) {
/* 195 */       throw new RuntimeException(e);
    }
  }

  private long readLong() {
    try {
/* 201 */       return readVarint(64);
/* 202 */     } catch (IOException e) {
/* 203 */       throw new RuntimeException(e);
    }
  }

  private String readString() {
    try {
/* 209 */       return this.m_in.readUTF();
/* 210 */     } catch (IOException e) {
/* 211 */       throw new RuntimeException(e);
    }
  }

  private byte readTag() {
    try {
/* 217 */       return this.m_in.readByte();
/* 218 */     } catch (IOException e) {
/* 219 */       throw new RuntimeException(e);
    }
  }

  protected long readVarint(int length) throws IOException {
/* 224 */     int shift = 0;
/* 225 */     long result = 0L;

/* 227 */     while (shift < length) {
/* 228 */       byte b = this.m_in.readByte();
/* 229 */       result |= (b & 0x7F) << shift;
/* 230 */       if ((b & 0x80) == 0) {
/* 231 */         return result;
      }
/* 233 */       shift += 7;
    }

/* 236 */     throw new RuntimeException("Malformed variable int " + length + "!");
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/classes/!/com/dianping/cat/home/service/client/transform/DefaultNativeParser.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */